package com.info.zhiduoduo.core.service.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.info.zhiduoduo.common.constants.Constants;
import com.info.zhiduoduo.common.utils.RandomUtil;
import com.info.zhiduoduo.core.formbean.crowdsource.AddCrowdsourceModel;
import com.info.zhiduoduo.core.service.CoreCrowdsourceService;
import com.info.zhiduoduo.repository.entity.crowdsource.Crowdsource;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceAccount;

import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CoreCrowdsourceServiceImpl implements CoreCrowdsourceService {


	@Override
	public Crowdsource addCrowdsource(AddCrowdsourceModel model) {
		Crowdsource crowdsource = new Crowdsource().setPhone(model.getPhone())
												   .setRegfrom(model.getRegfrom())
												   .setNickname(model.getRealname())
												   .setSex(model.getSex())
												   .setState(Constants.crowdsource_state_2);
		crowdsource.insert();

		new CrowdsourceAccount().setCrowdsourceid(crowdsource.getCrowdsourceid())
								.setBindcompid(model.getBindcompid())
								.setRealnameAuth(Constants.crowdsource_auth_3)
								.setRealname(model.getRealname())
								.setIdno(model.getIdno())
								.setAlipayLogonId(model.getAlipayLogonId())
								.setAcctno(model.getAcctno())
								.setAcctphone(model.getPhone())
								.setBankname(model.getBankname())
								.setContractNo(genContractNo())
								.setIsCheckBankCard(0)
								.insert();
		return crowdsource;
	}

	@Override
	public Crowdsource modifyCrowdsource(AddCrowdsourceModel model) {
		Crowdsource crowdsource = new Crowdsource().selectById(model.getUserid());
		if (null == crowdsource){
			return null;
		}
		crowdsource.setPhone(model.getPhone())
				.setRegfrom(model.getRegfrom())
				.setNickname(model.getRealname())
				.setSex(model.getSex());
		crowdsource.updateById();

		CrowdsourceAccount crowdsourceAccount = new CrowdsourceAccount()
				.selectOne(Condition.create().eq("crowdsourceid", model.getUserid()));
		crowdsourceAccount.setBindcompid(model.getBindcompid())
				.setRealname(model.getRealname())
				.setIdno(model.getIdno())
				.setAlipayLogonId(model.getAlipayLogonId())
				.setAcctno(model.getAcctno())
				.setAcctphone(model.getPhone())
				.setBankname(model.getBankname())
				.setIsCheckBankCard(0)
				.updateById();
		return crowdsource;
	}

	@Override
	public CrowdsourceAccount addCrowdsourceAccount(AddCrowdsourceModel model) {
		Crowdsource crowdsource = new Crowdsource().setPhone(model.getPhone())
												   .setRegfrom(model.getRegfrom())
												   .setNickname(model.getRealname())
												   .setSex(model.getSex())
												   .setState(Constants.crowdsource_state_2);
		crowdsource.insert();

		CrowdsourceAccount rowdsourceAccount = new CrowdsourceAccount().setCrowdsourceid(crowdsource.getCrowdsourceid())
																	   .setBindcompid(model.getBindcompid())
																	   .setRealnameAuth(Constants.crowdsource_auth_3)
																	   .setRealname(model.getRealname())
																	   .setIdno(model.getIdno())
																	   .setAlipayLogonId(model.getAlipayLogonId())
																	   .setAcctno(model.getAcctno())
																	   .setAcctphone(model.getPhone())
																	   .setBankname(model.getBankname())
																	   .setContractNo(genContractNo())
																	   .setIsCheckBankCard(0);
		rowdsourceAccount.insert();
		return rowdsourceAccount;
	}

	private String genContractNo() {
		return RandomUtil.createRandomNo("CROWD_", "");
	}
}
